<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Crypt;
use App\Http\Models\User;

class ManagerController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //加载电针仪用户列表页面
        $search = $request->input('search',''); //搜索条件，没有则默认为空
        $count = $request ->input('count',10); //每页显示几条数据，默认为10
        $request = $request->all();
        $custom = User::where('u_status',1)->where('u_name','like','%'.$search.'%')->paginate($count);
        return view('admin.lei.manager.index',['custom'=>$custom,'request'=>$request]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.lei.manager.add');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        $data = $request->except('_token');
        $rule = [
            'u_name'=>'required|between:5,12',
            'u_pass'=>'required|between:5,18',
            'u_email'=>'required|email',
            'u_phone'=>'required|between:11,11',
        ];
        $mess = [
            'u_name.required'=>'用户名必须输入',
            'u_name.between'=>'用户名的长度在5-12位之间',
            'u_pass.required'=>'密码必须输入',
            'u_pass.between'=>'密码的长度在5-18位之间',
            'u_email.required'=>'邮箱必须输入',
            'u_email.email'=>'邮箱的格式不正确',
            'u_phone.required'=>'电话必须输入',
            'u_phone.between'=>'电话的长度为11为',
        ];
        $validator = Validator::make($data,$rule,$mess);

        if($validator->fails()){
            return back()
                ->withErrors($validator)
                ->withInput();
        }
        if($data['u_pass'] != $data['u_rpass']){
            return back()->with('errors','俩次密码不一致');
        }
        $data['u_pass'] = Crypt::encrypt($data['u_pass']);
        $user = new User();
        $user->u_name = $data['u_name'];
        $user->u_pass = $data['u_pass'];
        $user->u_email = $data['u_email'];
        $user->u_phone = $data['u_phone'];
        $user->u_status = $data['u_status'];
        $user->u_dologin = $data['u_dologin'];
        $res = $user->save();
        //添加成功
        if($res){
            return redirect('admin/manager')->with('success','添加成功');
        }else{
            return back()->with('errors','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = user::find($id);
        if($user->u_dologin==0){
            $user->u_dologin = 1;
        }else{
            $user->u_dologin = 0;
        }
        $res = $user->save();
        if($res){
            return redirect('admin/manager');
        }else{
            return back();
        }

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = User::find($id);
        return view('admin.lei.manager.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $data = $request->except('_token');
        $rule = [
            'u_name'=>'required|between:5,12',
            'u_email'=>'required|email',
            'u_phone'=>'required|between:11,11',
        ];
        $mess = [
            'u_name.required'=>'用户名必须输入',
            'u_name.between'=>'用户名的长度在5-12位之间',
            'u_email.required'=>'邮箱必须输入',
            'u_email.email'=>'邮箱的的格式不正确',
            'u_phone.required'=>'手机号必须输入',
            'u_phone.between'=>'手机号长度为11位',
        ];
        $validator = Validator::make($data,$rule,$mess);

        if($validator->fails()){
            return back()
                ->withErrors($validator)
                ->withInput();
        }

        
        $user = user::find($id);
        $user->u_name = $data['u_name'];
        $user->u_email = $data['u_email'];
        $user->u_phone = $data['u_phone'];
        $res = $user->save();
        //添加成功
        if($res){
            return redirect('admin/manager')->with('success','修改成功');
        }else{
            return back()->with('errors','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       $data = [];
       $re =  User::find($id)->delete();
       //删除成功
       if($re){
            $data['status']= 0;
            $data['msg']='删除成功';
       }else{
           $data['status']= 1;
           $data['msg']='删除失败';
       }
       return $data;

    }
}
